package meijuli.com.join51.business.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.fragment.SetPayPwdFragment;
import com.jhx.common.model.Event;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import meijuli.com.join51.business.setpaypwd.PayPwdActivity;

/**
 * 设置交易密码
 *
 * @author wenqin 2017-05-06 14:57
 */

public class SetPayPwdActivity extends BaseActivity {
    TextView mToolbarTitle;
    Toolbar mToolbar;
    private int mPwdIsExist = PayPwdActivity.SET_PASSWORD;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_fragment);
        mToolbarTitle = findViewById(R.id.toolbar_title);
        mToolbar = findViewById(R.id.toolbar);
        if(getIntent() != null) {
            mPwdIsExist = getIntent().getIntExtra(PayPwdActivity.KEY_TYPE, 0);
        }
        initToolBar();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        mToolbarTitle = findViewById(R.id.toolbar_title);
        mToolbar = findViewById(R.id.toolbar);
        addFragment(SetPayPwdFragment.newInstance(mPwdIsExist));
    }

    private void initToolBar() {
        if(mPwdIsExist == PayPwdActivity.UPDATE_PASSWORD) {
            mToolbarTitle.setText("重置交易密码");
        } else {
            mToolbarTitle.setText("设置交易密码");
        }
        mToolbar.setNavigationIcon(R.mipmap.icon_back_white);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!getSupportFragmentManager().popBackStackImmediate()) {
                    finish();
                }
            }
        });
    }

    @Subscribe
    public void onEvent(Event event) {
            if(event.getMessage().equals(PayPwdActivity.mEventMessage)) {
                finish();
            }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

}
